package zheezes.eisp.collect.model.impl.gbw;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import zheezes.eisp.collect.model.VipFrozenCollectTaskHandler;
import zheezes.eisp.data.orm.ResMeter;
import zheezes.eisp.data.orm.VipFrozenDataPo;
import zheezes.protocol.Frame;
import zheezes.protocol.impl.gbw.DataUnit;
import zheezes.protocol.impl.gbw.GbwConAddr;
import zheezes.protocol.impl.gbw.GbwFrame;
import zheezes.protocol.impl.gbw.afn8d.Afn8DDownF2S0DataUnit;
import zheezes.protocol.impl.gbw.afn8d.Afn8DUpF2DataUnit;
import zheezes.protocol.impl.gbw.bean.VipFrozenData;
import zheezes.protocol.impl.gbw.bean.VipFrozenDataParam;
import zheezes.protocol.impl.gbw.format.DataForm20;
import zheezes.protocol.impl.gbw.format.DataForm24;
import zheezes.socket.Transput;

public class GbwVipFrozenCollectTaskHandler extends VipFrozenCollectTaskHandler {
	private static Logger logger = 
		Logger.getLogger(GbwVipFrozenCollectTaskHandler.class);
	
	@SuppressWarnings("unused")
	private int count = 0;

	public void run() {
		List<ResMeter> list = query();
		Calendar cal = Calendar.getInstance();
		cal.setTime(task.getDataDate());
		GbwConAddr con = new GbwConAddr(dataAccessor
				.getConById(task.getConId()).getLogicAddr());

		for (int i = 0; i < list.size() && !terminated; i++) {
			GbwFrame frame = new GbwFrame();
			frame.setDirection(GbwFrame.DIRECTION_DOWN);
			frame.setPrm(GbwFrame.PRM_STARTUP);
			frame.setConcentrator(con);
			frame.setAfn((byte) 0x8D);
			ResMeter meter = list.get(i);
			Afn8DDownF2S0DataUnit du = new Afn8DDownF2S0DataUnit();
			VipFrozenDataParam param = new VipFrozenDataParam();
			param.setSeq(meter.getSeq().shortValue());
			param.setDays((byte) 1);
			param.setSdate(new DataForm20(cal));
			du.addParam(param);
			frame.addDataUnit(du);

			// mailbox.clear();
			Transput transput;
			if((transput = postOffice.send(frame)) != null) {
				Frame[] recv = null;
				if (transput.await() == Transput.SUCCESS && (recv = transput.getRecv()) != null) {
					// Object message = null;
					for(Frame message : recv) {
					// while((message = mailbox.get()) != null) {
						save((GbwFrame) message);
					}
				}
			}
		}
		/* this codes was discard, because you shoud update some task properties after any task run
		if ((list.size() == 0) || (count > 0)) {
		}
		*/
		refreshTaskStat();
	}

	private void save(GbwFrame frame) {
		if (frame.getAfn() == (byte) 0x8D) {
			for (DataUnit du : frame.getDataUnits()) {
				if (du.hasData()) {
					if (du instanceof Afn8DUpF2DataUnit) {
						Afn8DUpF2DataUnit unit = (Afn8DUpF2DataUnit) du;
						for (VipFrozenData vfd : unit.getDatas()) {
							for (DataForm24[] dfs : vfd.getDatas()) {
								dataAccessor.beginTransaction();
								for (DataForm24 df : dfs) {
									if (df.getValue() != null) {
										VipFrozenDataPo po = new VipFrozenDataPo();
										po.setDate(df.getCal().getTime());
										po.setMpId(findBySeq(vfd.getSeq()).getMpId());
										po.setPp(df.getValue());
										if(dataAccessor.saveVipFrozenData(po) != 0) {
											dataAccessor.rollback();
											logger.info("Save vip hour frozen data failure");
											return;
										}
									} // if
								} // for
								dataAccessor.commit();
								count++;
							} // for
						}
					}
				}
			}
		}
	}
}
